package com.wanxi.tcp.demo1客户端发送一次数据服务器;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * TCP服务器端 接收一次客户端的信息，接收后就关闭服务器。
 */
public class TCPServer {
    public static void main(String[] args) throws IOException {
        System.out.println("tcp 服务端启动.....");
        //1. 创建服务器端socket
        ServerSocket serverSocket = new ServerSocket(30000);
        System.out.println("等待客户端的连接....");
        //2 建立客户端socket通道,阻塞形式
        Socket socket = serverSocket.accept();
        //3 创建输入流 建立soket通道,阻塞形式
        InputStream inputStream = socket.getInputStream();
//        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //4 接收上传的信息
//        String message = bufferedReader.readLine();
        StringBuilder message = new StringBuilder();
        int read = 0;
        while ((read = inputStream.read()) != -1) {
            message.append((char) read);
        }

        //5 输出信息
        System.out.println("有人说：" + message);
        socket.close();
        inputStream.close();
    }
}
